<?php

namespace plugin\ycMassage\app\api\controller;

use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageTravel;
use plugin\ycMassage\app\model\MassageUserTechnician;
use support\Request;

class TravelController extends BaseController
{
    public function list(Request $request)
    {
        $city_id = $request->post('city_id');
        if (!$city_id) {
            return $this->fail('请先选择地址');
        }
        $servive_time = $request->post('servive_time');
        if (!$servive_time) {
            return $this->fail('请先选择服务时间');
        }
        $now = date('H:i:s', strtotime($servive_time));
        $saas_appid = $request->saas_appid;
        $data = [];
        $technician_id = $request->post('technician_id');
        if ($technician_id) {
            $MassageTechnician = MassageUserTechnician::where(['uid' => $technician_id])->find();
            if ($MassageTechnician && $MassageTechnician->work_mch_id) {
                $where = [];
                $where[] = ['saas_appid', '=', $saas_appid];
                $where[] = ['city_id', '=', $city_id];
                $where[] = ['mch_id', '=', $MassageTechnician->work_mch_id];
                if ($MassageTechnician->work_shop_id) {
                    $where[] = ['shop_id', '=', $MassageTechnician->work_shop_id];
                }
                $MassageTravelMch = MassageTravel::where($where)->select();
                if ($MassageTravelMch) {
                    foreach ($MassageTravelMch as $item) {
                        if ($item->start_time && $item->end_time) {
                            if ($now < $item->start_time || $now > $item->end_time) {
                                continue;
                            }
                        }
                        $data[] = [
                            'type' => 'custom',
                            'id' => $item->id,
                            'name' => $item->title,
                            'start_distance' => $item->start_distance,
                            'start_price' => $item->start_price,
                            'mileage_price' => $item->mileage_price,
                            'virtually_mileage' => $item->virtually_mileage,
                            'commuting' => $item->commuting
                        ];
                    }
                }
            }
        }
        $MassageTravel = MassageTravel::where(['saas_appid' => $saas_appid, 'city_id' => $city_id])->whereNull('mch_id')->whereNull('shop_id')->select();
        if ($MassageTravel->isEmpty()) {
            $config = getHpConfig('', $saas_appid, 'travel');
            if (isset($config['taxi_state']) && $config['taxi_state']) {
                $data[] = [
                    'type' => 'taxi',
                    'name' => '出租车',
                    'start_distance' => $config['taxi_start_distance'],
                    'start_price' => $config['taxi_start_price'],
                    'mileage_price' => $config['taxi_mileage_price'],
                    'virtually_mileage' => $config['taxi_virtually_mileage'],
                    'commuting' => $config['taxi_commuting']
                ];
            }
            if (isset($config['bus_state']) && $config['bus_state']) {
                $isAdd = true;
                if (!empty($config['bus_time'])) {
                    list($start_time, $end_time) = $config['bus_time'];
                    if ($now < $start_time || $now > $end_time) {
                        $isAdd = false;
                    }
                }
                if ($isAdd) {
                    $data[] = [
                        'type' => 'bus',
                        'name' => '公交',
                        'start_distance' => 0,
                        'start_price' => $config['bus_price'],
                        'mileage_price' => 0,
                        'virtually_mileage' => 0,
                        'commuting' => $config['bus_commuting']
                    ];
                }
            }
        } else {
            foreach ($MassageTravel as $item) {
                if ($item->start_time && $item->end_time) {
                    if ($now < $item->start_time || $now > $item->end_time) {
                        continue;
                    }
                }
                $data[] = [
                    'type' => 'custom',
                    'id' => $item->id,
                    'name' => $item->title,
                    'start_distance' => $item->start_distance,
                    'start_price' => $item->start_price,
                    'mileage_price' => $item->mileage_price,
                    'virtually_mileage' => $item->virtually_mileage,
                    'commuting' => $item->commuting
                ];
            }
        }
        if (empty($data)) {
            return $this->fail('无可用出行方式');
        }
        return $this->successRes($data);
    }
}
